package com.gopay.bis.gdb.wap.impl;

import java.util.Date;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.gdb.wap.gfbank.payment.merchant.PayCfg;
import com.gopay.bis.gdb.wap.operation.GdbWapQuery;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.TransUtil;
import com.gopay.remote.bank.ICertHelper;
import com.gopay.remote.bank.businesses.BankService;
/**
 * 广发银行Wap网关支付
 * 
 * @GdbWapBankServiceImpl.java
 * @author lilili
 * @2017-03-23 上午9:26:20 www.gopay.com.cn Inc.All rights reserved.
 */
public class GdbWapBankServiceImpl implements BankService {
	private ICertHelper gdbWapCertHelper;
    private GdbWapQuery gdbWapQuery;
    @Override
    public Object sign(BaseMessage msg) throws GopayException {
        try {
        	GdbB2cMessage message = (GdbB2cMessage) msg;
            String merchid = message.getMerID();
            String orderid = message.getBankOrderId();
            String amount = message.getAmount().toString();
            Date date = new Date();
            String newDate=TransUtil.getDateStr("yyyyMMddHHmmss", date);
            String orderDate=newDate.substring(0, 8);
            String orderTime=newDate.substring(8);
            String gdbURL = PayCfg.getValue("gfbankpaygateurl"); // 广东发展银行网关支付地址
            String returl = message.getPayRuturnUrl(); // 接收支付结果通知地址

            String signResult = gdbWapCertHelper.sign(message);
            // 组装请求银行参数
			gdbURL = gdbURL + "?merId=" + merchid + "&orderNum=" + orderid
					+ "&orderAmt=" + amount + "&orderTime=" + orderTime + "&orderDate="
							+ orderDate +  "&signInfo=" + signResult + "&retUrl="
					+ returl;
            return gdbURL;
        } catch (Exception e) {
            BankLogUtils.printB2CSignExceptionLog(msg, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
        }

    }

    @Override
    public Object verify(BaseMessage msg) throws GopayException {
        return gdbWapCertHelper.verify(msg);
    }

    @Override
    public Object query(Object obj) throws GopayException {
        GdbB2cMessage message = (GdbB2cMessage) obj;
        return gdbWapQuery.query(message);
    }
    

	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	public void setGdbWapCertHelper(ICertHelper gdbWapCertHelper) {
		this.gdbWapCertHelper = gdbWapCertHelper;
	}

	public void setGdbWapQuery(GdbWapQuery gdbWapQuery) {
		this.gdbWapQuery = gdbWapQuery;
	}

}
